/**
 * 全员营销活动|我的二维码 cyy
 */
var departmentPersonalRanking = {
		token : null,
		init : function(accessToken){
			departmentPersonalRanking.token = accessToken;
			return departmentPersonalRanking;
		},
		// 当前员工排行
		currentStaffRanking : function(tabTime) {
			var url = departmentPersonalRanking.getPath()+"/aem/client/currentStaffRanking";
			console.log("---->"+departmentPersonalRanking.token);
	        $.post(url, {
	        	"token":departmentPersonalRanking.token,
	        	"tabTime":tabTime,
	        	"rankingType":"department",
	        }, function (data) {
	            if (data.status == "200" || data.status == "300") {
	            	var staffInfo = data.staffInfo;
	            	var rankingData = data.data;
	            	if(StringEmpty(staffInfo)){
	            		//$('.head').css("background-image","url(" + staffInfo.headImg + ")");
	            		$('#head-img').attr("src",staffInfo.headImg);
		            	$('#staffName').html(staffInfo.name);
		            	$('#staffCompany').html(staffInfo.departmentName);
		            	$(document).attr("title",staffInfo.departmentName);
	            	}
	            	
	            	if(StringEmpty(rankingData)){
	            		$('#accumulativeScore').html(rankingData.accumulativeScore);
	            		$('#scoreRanking').html(rankingData.scoreRanking);
	            	}else{
                        $('#accumulativeScore').html("0");
                        $('#scoreRanking').html("0");
					}
	            } else {
	            	//跳转到登录页
	            	/*window.location.href="";*/
	            	console.log(data);
	            }
	        });
		},
		// 员工排行
		staffRanking : function(pageIndex,tabTime){
			var url = departmentPersonalRanking.getPath()+"/aem/client/staffRanking";
	        $.post(url, {
	        	"token":departmentPersonalRanking.token,
	        	"index":pageIndex,
	        	"size":10,
	        	"tabTime":tabTime,
	        	"rankingType":"department",
	        	"flagIndex":flagIndex,
	        }, function (data) {
	            if (data.status == "200") {
	            	if(data.message == "flag"){
	            		zeroFlag = true;
	            	}
	            	var rankingData = data.data;
	            	var heroHtml="";
	            	var rankingHtml="";
	            	for(var i = 0;i<rankingData.length;i++){
	            		console.log("rankingData[i].name--->"+rankingData[i].name);
	            		
	            		var accumulativeScore = 0;
	            		if(StringEmpty(rankingData[i].accumulativeScore)){
	            			accumulativeScore = rankingData[i].accumulativeScore;
	            		}
	            		if(scoreRanking<4){
	            			heroHtml += '<div data-v-43210d38="" class="hero-rank">';
	            			heroHtml += '<img data-v-43210d38="" src="'+rankingData[i].headImg+'" class="avatar">';
	            			heroHtml += '<p data-v-43210d38="" class="name">'+rankingData[i].name+'</p>';
	            			/*heroHtml += '<p data-v-43210d38="" class="address">'+rankingData[i].departmentName+'</p>';*/
	            			heroHtml += '<p data-v-43210d38="" class="score">+ <span data-v-43210d38="">'+accumulativeScore+'</span> 积分</p>';
	            		    if(scoreRanking == 1){
	            		    	heroHtml += '<img data-v-43210d38="" src="" class="rank-icon"></div>';
	            		    }else if(scoreRanking == 2){
	            		    	heroHtml += '<img data-v-43210d38="" src="" class="rank-icon"></div>';
	            		    }else{
	            		    	heroHtml += '<img data-v-43210d38="" src="" class="rank-icon"></div>';
	            		    }
	            		}else{
	            			if(StringEmpty(rankingData[i].scoreRanking)){
                                rankingHtml += '<li><div class="head-bg"><span class="number">'+rankingData[i].scoreRanking+'</span>';
							}else{
                                rankingHtml += '<li><div class="head-bg"><span class="number">'+scoreRanking+'</span>';
							}
		            		rankingHtml += '<img src="'+rankingData[i].headImg+'" class="head">';
		            		rankingHtml += '<span style="margin-top: 8px;" class="name">'+rankingData[i].name+'</span>';
		            		/*rankingHtml += '<span class="name" style="margin-top:30px">'+rankingData[i].departmentName+'</span>';
	*/	            		
		            		if(!StringEmpty(rankingData[i].accumulativeScore)){
		            			rankingHtml += '<div class="integral">+<span>0</span>积分</div></div></li>';
		            		}else{
		            			rankingHtml += '<div class="integral">+<span>'+accumulativeScore+'</span>积分</div></div></li>';
		            		}
	            		}
	            		scoreRanking++;
	            	}
	            	if(pageIndex == 1){
	            		$('.hero-list').html(heroHtml);
		            	$('.rank-list').html(rankingHtml);
	            	}else{
	            		$('.rank-list').append(rankingHtml);
	            	}
	            	
	            } else if(data.status == "300"){
	            	if(pageIndex == 1){
	            		$('.hero-list').html("");
		            	$('#personalRankingList').html("");
	            	}
	            	loadValid = false ; 
	            }else{
	            	//跳转到登录页
	            	/*window.location.href="";*/
	            	console.log(data);
	            }
	        });
		},
		
		getPath : function(){ 
			return $('#servicePath').attr("servicePath");
		},
		parseUrl : function(url) {
	        var a =  document.createElement('a');
	        a.href = url;
	        return {
	            source: url,
	            protocol: a.protocol.replace(':',''),
	            host: a.hostname,
	            port: a.port,
	            query: a.search,
	            params: (function(){
	                var ret = {},
	                    seg = a.search.replace(/^\?/,'').split('&'),
	                    len = seg.length, i = 0, s;
	                for (;i<len;i++) {
	                    if (!seg[i]) { continue; }
	                    s = seg[i].split('=');
	                    ret[s[0]] = s[1];
	                }
	                return ret;
	            })(),
	            file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
	            hash: a.hash.replace('#',''),
	            path: a.pathname.replace(/^([^\/])/,'/$1'),
	            relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
	            segments: a.pathname.replace(/^\//,'').split('/')
	        };
	    }
		
}
var pageIndex = 1;
var pageSize = 10;
var tabTime = 1;
var loadValid = true;
var flagIndex = 1;
var zeroFlag = false;
var scoreRanking = 1;
// 昨天
$('#one').click(function(){
	loadValid = true;
	zeroFlag = false;
	pageIndex = 1;
	tabTime = 1;
	flagIndex = 1;
	scoreRanking = 1;
	$('.mint-tab-item').removeClass("is-selected");
	$('#one').addClass("is-selected");
	$('.title').html("昨日英雄榜Top3");
	$('.departmentPersonalRankingTip').html("近1天排名");
	departmentPersonalRanking.currentStaffRanking(tabTime);
	departmentPersonalRanking.staffRanking(pageIndex,tabTime);  
});
//近七天
$('#senven').click(function(){
	loadValid = true;
	zeroFlag = false;
	pageIndex = 1;
	tabTime = 7;
	flagIndex = 1;
	scoreRanking = 1;
	$('.mint-tab-item').removeClass("is-selected");
	$('#senven').addClass("is-selected");
	$('.title').html("本周英雄榜Top3");
	$('.departmentPersonalRankingTip').html("近7天排名");
	departmentPersonalRanking.currentStaffRanking(tabTime);
	departmentPersonalRanking.staffRanking(pageIndex,tabTime);  
});
//近30天
$('#month').click(function(){
	loadValid = true;
	zeroFlag = false;
	pageIndex = 1;
	tabTime = 30;
	flagIndex = 1;
	scoreRanking = 1;
	$('.mint-tab-item').removeClass("is-selected");
	$('#month').addClass("is-selected");
	$('.title').html("本月英雄榜Top3");
	$('.departmentPersonalRankingTip').html("近30天排名");
	departmentPersonalRanking.currentStaffRanking(tabTime);
	departmentPersonalRanking.staffRanking(pageIndex,tabTime);  
});
//总榜
$('#all').click(function(){
	loadValid = true;
	zeroFlag = false;
	pageIndex = 1;
	tabTime = "";
	flagIndex = 1;
	scoreRanking = 1;
	$('.mint-tab-item').removeClass("is-selected");
	$('#all').addClass("is-selected");
	$('.title').html("英雄榜总榜Top3");
	$('.departmentPersonalRankingTip').html("总排名");
	departmentPersonalRanking.currentStaffRanking(tabTime);
	departmentPersonalRanking.staffRanking(pageIndex,tabTime);  
});

//监听是否滑到底部
window.addEventListener('scroll', throttle(loadData, 500, {leading:false}), false);

//加载更多数据
function loadData(){
	if($(window).scrollTop() + $(window).height() >= $(document).height()&&loadValid){
		if(zeroFlag){
			flagIndex++;
		}
		pageIndex++;
		
		departmentPersonalRanking.staffRanking(pageIndex,tabTime); 
	}
}
//频率控制函数
function throttle(fn,delay,options) {
     var wait=false;
     if (!options) options = {};
     return function(){
         var that = this,args=arguments;
         if(!wait){
             if (!(options.leading === false)){
                 fn.apply(that,args);
             }
             wait=true;
             setTimeout(function () {
                 if (!(options.trailing === false)){
                     fn.apply(that,args);
                 }
                 wait=false;
             },delay);
         }
     }
 }

